//
//  PhotoContainer.m
//  ChangJia
//
//  Created by 张育铭 on 16/9/12.
//  Copyright © 2016年 Zhangyuming. All rights reserved.
//

#import "PhotoContainer.h"
#import "SDPhotoBrowser.h"
#import <SDAutoLayout/SDAutoLayout.h>
#import <SDWebImage/SDWebImage.h>

@interface PhotoContainer ()<SDPhotoBrowserDelegate>

@property (nonatomic, strong) NSArray *imageViewsArray;

@end

@implementation PhotoContainer

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        NSMutableArray *temp = [NSMutableArray new];
        for (int i = 0; i < 9; i++) {
            UIImageView *imageView = [UIImageView new];
            imageView.image = nil;
            imageView.tag = i;
            imageView.hidden = YES;
            imageView.userInteractionEnabled = YES;

            imageView.contentMode         = UIViewContentModeScaleAspectFill;
            imageView.clipsToBounds       = YES;
            [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)]];
            [temp addObject:imageView];
            [self addSubview:imageView];
        }
        self.imageViewsArray = [temp copy];
    }
    return self;
}

- (void)setPicPathStringsArray:(NSArray *)picPathStringsArray {
    _picPathStringsArray = picPathStringsArray;

    if (_picPathStringsArray.count == 0) {
        self.height_sd = 0;
        self.fixedHeight = @(0);
        return;
    }
    
    CGFloat itemW = [UIScreen mainScreen].bounds.size.width > 320 ? 100 : 80;
    CGFloat itemH = itemW;

    long perRowItemCount = [self perRowItemCountForPicPathArray:_picPathStringsArray];
    CGFloat margin = 5;
    
    for (UIImageView *imv in _imageViewsArray) {
        imv.hidden = YES;
    }
    
    [_picPathStringsArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        long columnIndex = idx % perRowItemCount;
        long rowIndex    = idx / perRowItemCount;
        
        UIImageView *imageView = [_imageViewsArray objectAtIndex:idx];
        imageView.hidden = NO;
        [imageView sd_setImageWithURL:picPathStringsArray[idx] placeholderImage:[UIImage imageNamed:@"moment_default"]];
        imageView.frame  = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
    }];

    CGFloat w = perRowItemCount * itemW + (perRowItemCount - 1) * margin;
    int columnCount = ceilf(_picPathStringsArray.count * 1.0 / perRowItemCount);
    CGFloat h = columnCount * itemH + (columnCount - 1) * margin;
    
    self.width_sd    = w;
    self.height_sd   = h;
    self.fixedHeight = @(h);
    self.fixedWidth  = @(w);
}

- (NSInteger)perRowItemCountForPicPathArray:(NSArray *)array {
    if (array.count < 3) {
        return array.count;
    } else if (array.count <= 3) {
        return 3;
    } else {
        return 3;
    }
}

- (void)tapImageView:(UITapGestureRecognizer *)tap {
    UIView *imageView = tap.view;
    SDPhotoBrowser *browser   = [[SDPhotoBrowser alloc] init];
    browser.currentImageIndex = imageView.tag;
    browser.sourceImagesContainerView = self;
    browser.imageCount = self.picPathStringsArray.count;
    NSLog(@"%@",self.picPathStringsArray);
    browser.delegate = self;
    [browser show];
}

#pragma mark - SDPhotoBrowserDelegate
// 返回高质量图片的url
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
    NSString *urlStr = self.picBigStringsArray[index];
    return [NSURL URLWithString:urlStr];
}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
    UIImageView *imageView = self.subviews[index];
    return imageView.image;
}


@end
